package com.youdianstar.app.ui.pay

import androidx.lifecycle.viewModelScope
import com.youdianstar.app.entity.*
import com.youdianstar.app.support.ApiRepository
import com.youdianstar.app.common.base.BaseViewModel
import com.youdianstar.app.entity.CouponRes
import com.youdianstar.app.common.event.NetStateLiveData
import com.youdianstar.app.common.util.StrUtil

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


/**
 * File: PaymentViewModel
 * Author: 82149 Create: 2021/11/25 10:01
 * Changes (from 2021/11/25)
 * --------------------------------------------------
 * description: 钱包模块
 * ---------------------------------------------------
 *
 */
class PaymentViewModel(private val repo: ApiRepository) : BaseViewModel() {

    val addResultLiveData = NetStateLiveData<Any>()

    /**
     * 添加卡 后端绑定
     *
     */
    fun bindCard(req: CardBindReq) {
        viewModelScope.launch (Dispatchers.IO){
            repo.bindCard(req,addResultLiveData)
        }
    }

    var isPaying = false
    val paymentResultLiveData = NetStateLiveData<PaymentResult>()

    /**支付*/
    fun payment(req : PaymentReq, isShow :Boolean = true){
        if (isPaying)
            return
        if (isShow)
            showLoading()
        isPaying = true
        viewModelScope.launch (Dispatchers.IO){
            repo.payment(req,paymentResultLiveData)
        }
    }



    val methodResultLiveData = NetStateLiveData<PaymentMethodResult>()

    /**
     * 获取支付方式
     *
     * @param bisType 1-租借 3-会员 5-押金 7-报失 9-添加卡 11-优惠券 13-购买 15-置换 16-预付款
     */
    fun getPaymentMethod(bisType : Int, amount :Double,orderNo: String?){
        showLoading()
        viewModelScope.launch (Dispatchers.IO){
            repo.getPaymentMethod(bisType,amount,if (StrUtil.isEmpty(orderNo)) null else orderNo,methodResultLiveData)
        }
    }

    val priceResultLiveData = NetStateLiveData<OrderInfo>()

    /**
     * 计算支付金额
     *
     * @param type 流水类型：1-租借 3-会员 5-押金 7-报失 11-优惠券 13-购买 15-置换 17-预付款 19-充电卡
     */
    fun getPaymentPrice(type: Int,orderNo:String?, userCouponId :Int? = null,chargeCardId :Int? = null,isShow: Boolean = false){
        if (isShow)
            showLoading()
        viewModelScope.launch (Dispatchers.IO){
            repo.getPaymentPrice(type,orderNo,userCouponId,chargeCardId,priceResultLiveData)
        }
    }

    val receiptResultLiveData = NetStateLiveData<Any>()
    /**
     * 发送发票
     */
    fun sendPaymentReceipt(email : String,orderNo:String){
        showLoading()
        viewModelScope.launch (Dispatchers.IO){
            repo.sendReceipts(ReceiptReq(orderNo = orderNo, receiptEmail = email),receiptResultLiveData)
        }
    }


    //优惠券列表 - 账户记录
    val couponResLiveData = NetStateLiveData<List<CouponRes>>()

    /**
     * 获得优惠券列表-支付使用
     *
     * @param orderNo 订单
     */
    fun getCouponsList(orderNo: String,isShow: Boolean){
        if (isShow)
            showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.getCouponList(orderNo,couponResLiveData)
        }
    }

}